// pages/information/information.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        gender:['男','女'],
        form:{
          gender:"请选择性别",
          birthday:"请选择生日",
          address:"请选择居住地",
          image:"",
          text:"",
          name:"",
        },
        username:"",
        imageAddress:""
      },
      onLoad(options){
          this.setData({
            username:wx.getStorageSync('username')
          });
      },
      //表单
      getForm(event){
        const{filed} = event.currentTarget.dataset;
        const{value} = event.detail;
        this.setData({
          ["form."+ filed]:value
        });
        console.log(this.data.form);
      },
      //选择性别 || 学历
      changSelector(event){
        const {name}= event.currentTarget.dataset
        const {value} = event.detail
        this.setData({
          ["form."+name]:this.data[name][value]
        });
      },
      // 选择生日
      changBirthday(event){
        const {name}= event.currentTarget.dataset
        const {value} = event.detail
        this.setData({
          ["form."+name]:value
        });
      },
      //修改信息
      changeMsg(){
        wx.showModal({
          title: '提示',
          content: '你确认要修改吗？',
          complete: (res) => {
            if (res.cancel) {
            }
            if (res.confirm) {
                const {gender,birthday,address,image,text,name} = this.data.form;
                let object = {
                    username:this.data.username,
                    sex:gender,
                    autograph:text,
                    birthday:birthday,
                    address:address.join('/'),
                    imageUrl:image,
                    nickName:name
                }
              wx.request({
                url: 'http://localhost:8081/user/updataUserMsg',
                data:object,
                method:"POST",
                success:(result)=>{
                    const {code} = result.data;
                    if(code===200){
                        wx.switchTab({
                          url: '/pages/myself/myself',
                        })
                    } 
                }
              })
            }
          }
        })
      },
      //上传头像
      fileUpLoad(){
          const that = this;
        // --调用小程序api接口
        wx.chooseImage({
            count: 1, // 最多可以选择的图片张数，默认1
            sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
            sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
            success: function (res) {
              // 后台慢慢保存文件到本地      
              var tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                  url: 'http://localhost:8081/img/upImgs',
                  header: {
                    "Content-Type": "multipart/form-data"
                  },
                  filePath: tempFilePaths[0],
                  name: 'file',
                  formData: {
                    desc:'图片'
                  },
                  success:(result)=>{
                      const image = result.data;
                      let stringArray = image.split('/');
                      that.setData({
                          "form":{
                              "image":image,
                              "gender":"请选择性别",
                              "birthday":"请选择生日",
                              "address":"请选择居住地",
                              "text":"",
                              "name":"",
                          },
                          imageAddress:stringArray[stringArray.length-1]
                      });
                  }
                })
            }
          });
    }
})